package com.relman.business {
    import mx.rpc.IResponder;
    import com.relman.vo.*;
    import com.relman.util.ServiceUtils;
    
    import com.relman.util.DebugUtils;

    public class RelmanDelegate {
        private var _responder : IResponder;
        
        public function RelmanDelegate(responder : IResponder) {
            _responder = responder;
        }
        
        public function listManagers() : void {
        	ServiceUtils.send("/managers/list_xml", _responder);
        }
        
        public function listProjects(manager : ReleaseManagerVO) : void {
        	var url : String = "/managers/list_projects_xml/"  + buildManagerId(manager);
            ServiceUtils.send(url, _responder);
        }
        
        private function buildManagerId(manager : ReleaseManagerVO) : String {
        	var ext : String = manager.location.toString();
        	return ext;
        }
        
        public function listInstallers(manager : ReleaseManagerVO) : void {
        	var url : String = "/managers/list_installers_xml/" + buildManagerId(manager);
        	ServiceUtils.send(url, _responder);
        }
        
        public function listBuilders() : void {
        	ServiceUtils.send("/managers/list_installers_xml", _responder);
        }
        
        public function listEnvironments(manager : ReleaseManagerVO, project : ProjectVO) : void {
        	var url : String = "/managers/list_environments_xml/" + buildProjectExtUrl(manager, project);
        	ServiceUtils.send(url, _responder);
        }
        
        private function buildProjectExtUrl(manager : ReleaseManagerVO, project : ProjectVO) : String {
        	var ext : String = buildManagerId(manager) + "[project][" + project.name + "]";
        	return ext;
        }
		
		public function queryTransaction(manager : ReleaseManagerVO, transaction : TransactionVO) : void {
			var url : String = "/managers/query_transaction_xml/" + buildTransExtUrl(manager, transaction);
            ServiceUtils.send(url, _responder);
		}
		
		public function queryHistoryForTransaction(manager : ReleaseManagerVO, transaction : TransactionVO) : void {
			var url : String = "/managers/query_history_for_transaction_xml/" + buildTransExtUrl(manager, transaction);
			ServiceUtils.send(url, _responder);
		}
		
		private function buildTransExtUrl(manager : ReleaseManagerVO, transaction : TransactionVO) : String {
			var ext : String = buildManagerId(manager) + "[transaction][" + transaction.id.toString() + "]";
			return ext;
		}
		
		private function buildProjectEnvExtUrl(manager : ReleaseManagerVO, project : ProjectVO, env : EnvironmentVO) : String {
			var ext : String = buildManagerId(manager) + "[" + project.name + "][" + env.name + "]";
			return ext;
		}

        public function installProject(project : ProjectVO, manager : ReleaseManagerVO, env : EnvironmentVO, trunk : String, branch : String, rev : String) : void
        {
            var params : Object = new Object();
            params['manager'] = manager.location;
            params['project'] = project.name;
            params['env'] = env.name;
            params['trunk'] = trunk;
            params['branch'] = branch;
            params['revision'] = rev;
            params['_method'] = "POST";
            var url : String = "/managers/install_xml/" + buildProjectEnvExtUrl(manager, project, env);
            ServiceUtils.send(url, _responder, params);
        }

    }
}
